#include "widget.h"
#include "ui_widget.h"

#include <QTableWidgetItem>
#include <math.h>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->userlist = {
        {1, "关羽", "男", 30},
        {2, "赵云", "男", 22},
        {3, "孙尚香", "女", 18},
        {4, "貂蝉", "女", 20},
        {5, "张飞", "男", 20},
        {6, "周瑜", "男", 30},
        {7, "黄盖", "男", 22},
        {8, "小乔", "女", 18},
        {9, "大乔", "女", 20},
        {10, "夏侯惇", "男", 20},
        {11, "张辽", "男", 30},
        {12, "诸葛亮", "男", 22},
        {13, "孙尚香1", "女", 18},
        {14, "貂蝉1", "女", 20},
        {15, "张飞1", "男", 20}
    };

    // 初始化表格
    ui->tableWidget->setRowCount(this->pageSize);
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setHorizontalHeaderLabels({"编号", "姓名", "性别", "年龄"});

//    ui->tableWidget->setItem(0, 0, new QTableWidgetItem("10001"));
//    ui->tableWidget->setItem(0, 1, new QTableWidgetItem("关羽"));
//    ui->tableWidget->setItem(0, 2, new QTableWidgetItem("男"));
//    ui->tableWidget->setItem(0, 3, new QTableWidgetItem("30"));

    // 初始化
    ui->prevBtn->setEnabled(false);
    this->totalPage = ceil(this->userlist.size() / this->pageSize);

    this->updateComboBox();  // 根据页数更新ComboBox
    this->showTableData();

    // 设置LineEdit 只能输入整数 以及 有效的输入值范围
    QIntValidator *intValidator = new QIntValidator(1, this->userlist.size(), this);
    ui->lineEdit->setValidator(intValidator);

    connect(ui->prevBtn, &QPushButton::clicked, [this](){
        this->pageno--;
//        if(this->pageno == 1)
//        {
//            ui->prevBtn->setEnabled(false);
//        }
//        else
//        {
//            ui->prevBtn->setEnabled(true);
//            ui->nextBtn->setEnabled(true);
//        }
        this->setButtonState();
        this->showTableData();
        this->updateComboBox();
    });

    connect(ui->nextBtn, &QPushButton::clicked, [this](){
        this->pageno++;

//        if(this->pageno == this->totalPage)
//        {
//            ui->nextBtn->setEnabled(false);
//        }
//        else
//        {
//            ui->nextBtn->setEnabled(true);
//            ui->prevBtn->setEnabled(true);
//        }
        this->setButtonState();
        this->showTableData();
        this->updateComboBox();
    });
}

void Widget::showTableData()
{
    ui->tableWidget->clear();
    ui->tableWidget->setHorizontalHeaderLabels({"编号", "姓名", "性别", "年龄"});
    int row = 0;
    int start = (this->pageno - 1) * this->pageSize;
    int end = this->pageno * this->pageSize - 1;

    for(int i = start; i <= end; i++)
    {
        if(i > this->userlist.size() - 1)
        {
            break ;
        }
        UserInfo item = this->userlist.at(i);
        ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(item.id)));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(item.name));
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(item.gender));
        ui->tableWidget->setItem(row++, 3, new QTableWidgetItem(QString::number(item.age)));
    }
}

void Widget::updateComboBox()
{
    ui->comboBox->clear();
    for(int i = 1; i <= this->totalPage; i++)
    {
        ui->comboBox->addItem(QString("第 %1 页").arg(i));
    }
    ui->comboBox->setCurrentIndex(this->pageno - 1);
}

void Widget::setButtonState()
{
    // 判断第一页
    if (this->pageno == 1)
    {
        ui->prevBtn->setEnabled(false);
    }
    else
    {
        ui->prevBtn->setEnabled(true);
    }

    // 判断最后一页
    if (this->pageno == this->totalPage)
    {
        ui->nextBtn->setEnabled(false);
    }
    else
    {
        ui->nextBtn->setEnabled(true);
    }
}


Widget::~Widget()
{
    delete ui;
}

void Widget::on_comboBox_activated(int index)
{
    this->pageno = index + 1;
    this->setButtonState();
    this->showTableData();
}

void Widget::on_lineEdit_editingFinished()
{
    int pageNum = ui->lineEdit->text().toInt();
    if(pageNum > this->userlist.size())
    {
        return;
    }
    else
    {
        this->pageSize = pageNum;
        ui->tableWidget->setRowCount(this->pageSize);
        this->totalPage = ceil(this->userlist.size() / this->pageSize);
        this->updateComboBox();  // 更新ComboBox
        this->setButtonState();  // 更新按钮状态
        this->showTableData();  // 重新显示数据
    }
}
